import { readFile } from '../../../src/utils';
import { Coverage } from '../../../src/test-parser';
import { PhpUnitTestParser } from '../../../src/parsers/php-unit-test/php-unit-test-parser';
import { expect } from 'chai';

describe('PhpUnitTestParser', () => {
    let parser: PhpUnitTestParser;

    beforeEach(() => {
        parser = new PhpUnitTestParser();
    });

    describe('count', () => {
        it('should correctly calculate coverage from the HTML content', async () => {
            const htmlContent = await readFile('./test/parsers/php-unit-test/phpUnitTest-result.html');
            const expectedCoverage: Coverage = {
                total: 2,
                passed: 1,
                failed: 1,
                skipped: 0,
                passedRate: 50
            };

            const res = await parser.count(htmlContent);
            expect(res).to.deep.equal(expectedCoverage);
        });
    });

    describe('count method', () => {
        it('should correctly calculate coverage from the HTML content2', async () => {
            const htmlContent = await readFile('./test/parsers/php-unit-test/phpUnitTest-result2.html');
            const expectedCoverage: Coverage = {
                total: 5,
                passed: 5,
                failed: 0,
                skipped: 0,
                passedRate: 100
            };

            const res = await parser.count(htmlContent);
            expect(res).to.deep.equal(expectedCoverage);
        });
    });

    describe('File Path Converters', () => {
        describe('coverageFile', () => {
            it('should throw an error if the path does not end with .html', async () => {
                try {
                    const invalidPath = 'path/to/report.json';
                    await parser.coverageFile(invalidPath);
                    expect.fail('Expected error was not thrown');
                } catch (error) {
                    expect(error).to.be.an('error');
                    // @ts-ignore
                    expect(error.message).to.equal(`Input variable 'reportPath' should reference a report with html extension`);
                }
            });

            it('should return the correct path if it ends with .html and exists', async () => {
                const validPath = './test/parsers/php-unit-test/phpUnitTest-result.html';
                const result = await parser.coverageFile(validPath);
                expect(result).to.equal(validPath);
            });
        });

        describe('htmlFile', () => {
            it('should return the same path', () => {
                const path = './test/parsers/php-unit-test/phpUnitTest-result.html';
                expect(parser.htmlFile(path)).to.equal(path);
            });
        });
    });
});
